// //  计数器案例
import { useState } from "react"
// 函数组件 看起来更舒服
export default function Count() {
  const stateHook = useState({ count: 1 })
  const [state, setState] = stateHook
  const changeCount = (num) => {
    const newCount = state.count + num
    if (newCount <= 0) return
    setState({
      count: newCount
    })
    document.title = newCount
  }
  return (
    <div>
      <button onClick={() => changeCount(-1)}>-1</button>
      <div>{state.count}</div>
      <button onClick={() => changeCount(+1)}>+1</button>
    </div>
  )
}

/**
 * 分割线 类实现 代码和复杂度都高*/
// //
// import {PureComponent} from "react";
//
// export default class Count extends PureComponent {
//   constructor() {
//     super();
//     this.state = {
//       count: 1,
//     }
//   }
//
//   changeCount(num) {
//     const {count} = this.state;
//     const newCount = count + num;
//     if(newCount <= 0) return;
//     this.setState({
//       count: newCount,
//     })
//     document.title = newCount;
//   }
//
//   render() {
//     const {count} = this.state;
//     return <div>
//       <button onClick={() => this.changeCount(-1)}>-1</button>
//       <div>{count}</div>
//       <button onClick={() => this.changeCount(+1)}>+1</button>
//     </div>
//   }
// }
